#include "ET++.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "InspItem.h"

#include "Class.h"
#include "String.h"
#include "Alert_e.h"
#include "System.h"
#include "Reference.h"
#include "ClassManager.h"
#include "EtPeCmdNo.h"

//---- InspectorItem0 ----------------------------------------------------------

NewMetaImpl0(PeInspectorItem0,TextItem);

static Ink *modifiedInk= 0;

PeInspectorItem0::PeInspectorItem0(const char *n, GrFace fc)
							: TextItem("", gFixedFont->WithFace(fc), Point(2,1))
{
	name= 0;
	died= FALSE;
	modified= FALSE;
	if (n)
		strreplace(&name, n);
}

PeInspectorItem0::~PeInspectorItem0()
{
	SafeDelete(name);
}

void PeInspectorItem0::Deref(bool)
{
}

void PeInspectorItem0::Update()
{
}

void PeInspectorItem0::Died()
{
	died= TRUE;
}

void PeInspectorItem0::Draw(Rectangle)
{
	if (text && *text) {
		Point p= contentRect.origin+border;
		p.y+= font->Ascender();
		Ink *ink= ePatBlack;
		if (died)
			ink= ePatGrey50;
		else if (modified) {
			if (modifiedInk == 0) {
				if (GrHasColor())
					modifiedInk= new_RGBColor(255, 0, 0);
				else
					modifiedInk= ePatBlack;
			}
			ink= modifiedInk;
		}
		GrShowString(font, ink, p, (byte*)text);
	}
}

//---- PeClassItem0 ------------------------------------------------------------

NewMetaImpl0(PeClassItem0, PeSuperItem0);

PeClassItem0::PeClassItem0(void *a, const char *name, GrFace f)
														: PeSuperItem0(name, f)
{
	addr= a;
}

void PeClassItem0::Update()
{
	SetFString(FALSE, "%s: (0x%x)", name, (unsigned)addr);
}

//---- SuperItem0 --------------------------------------------------------------

NewMetaImpl0(PeSuperItem0, PeInspectorItem0);

PeSuperItem0::PeSuperItem0(const char *name, GrFace f) : PeInspectorItem0(name, f)
{
}

void PeSuperItem0::Deref(bool expanded)
{
	int what= expanded ? cPeLoadRefNew : cPeLoadRef;
	Object *isa= gClassManager->Find(name);
	if (isa)
		Control(GetId(), what, new Ref(*isa));
	else
		ShowAlert(eAlertNote, "no isa link");
}

void PeSuperItem0::Update()
{
	SetFString(FALSE, "%s:", name);
}

//---- PeInspectorItem ---------------------------------------------------------

NewMetaImpl0(PeInspectorItem, PeInspectorItem0);

PeInspectorItem::PeInspectorItem(const char *n, Ref *r)
											: PeInspectorItem0(n, eFacePlain)
{
	ref= r;
	Enable(r->CanDeref(), FALSE);
}

PeInspectorItem::~PeInspectorItem()
{
	SafeDelete(ref);
}

void PeInspectorItem::Deref(bool expanded)
{
	int what= expanded ? cPeLoadRefNew : cPeLoadRef;
	Ref *r= ref->Deref();
	if (r == 0)
		ShowAlert(eAlertNote, "can't deref instance variable \"%s\"", name);
	else
		Control(GetId(), what, r);
}

void PeInspectorItem::Update()
{
	char *newValue= ref->Value(name);
	if (text && strlen(text) && strcmp(newValue, text) != 0)
		modified= TRUE;
	else
		modified= FALSE;
	SetString(newValue);
}

